package component
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import mx.containers.Box;
	import mx.containers.BoxDirection;
	import mx.containers.HBox;
	import mx.controls.Image;
	import mx.controls.Text;
	import mx.core.IToolTip;
	import mx.core.UIComponent;
	import mx.utils.StringUtil;

    public class HTMLToolTip extends Box implements IToolTip
    {
    	public var messages:String="";
    	public var serverMessages:String="";
    	public var closeEvent:String= MouseEvent.MOUSE_OUT;
		public var delay:int = 5000;
		public var displayEvent:String =MouseEvent.MOUSE_OVER;
		public var closeServerEvent:String=MouseEvent.MOUSE_OUT;
		public var displayServerEvent:String =MouseEvent.MOUSE_OVER;
		public var control:UIComponent;
		public var timerIsActivated:Boolean=false;
		private var timerKeyHide:Timer;
		public var borderStyle:String = null;
		public var borderColor:String= null;
		public var backgroundColor:String= null;  
		public var messagesError:Array=new Array();
		public var messagesInfo:Array=new Array();
		public var messagesWarn:Array=new Array();
		
		[Embed(source="images/red.png")]
        public var errorClass:Class; 
        
        [Embed(source="images/yellow.png")]
        public var warnClass:Class; 
         
        [Embed(source="images/blue.png")]
        public var infoClass:Class;
         
       
        protected var img:Image;
        protected var textField:Text;
        
        private var _text:String = '';
        
        public function HTMLToolTip()
        {   
        	super();
            mouseChildren=false;
            horizontalScrollPolicy="off";
            verticalScrollPolicy="off";
            
            direction = BoxDirection.VERTICAL;
            
         }

        public function removeControlsEvents():void
        {
        	if(control != null)
        	{
        		closeToolTip(null);
        		if(closeEvent != null){
        		   control.removeEventListener(closeEvent,closeToolTip);
        		}
        		if(displayEvent != null){
        		   control.removeEventListener(displayEvent,openTooltip);
        		}
        	}
        }
        public function  initCustomEvents():void
        {
        	if(control != null)
        	{
        		closeToolTip(null);
        		if(closeEvent == null)
        		{
        			closeEvent = MouseEvent.MOUSE_OUT;
        		}
        		if(displayEvent == null)
        		{
        			displayEvent == MouseEvent.MOUSE_OVER;
        		}
        		
        		if(closeEvent != null){
        			if(closeEvent != "delay"){
	                        control.addEventListener(closeEvent,closeToolTip);
	                     }          
	                     else
	                     {
	                     	activateTimer();
	                     }       		   
        		}
        		
        		if(displayEvent != null){
        		   control.addEventListener(displayEvent,openTooltip);
        		}
        	}
        }
        public function removeControlsServerEvents():void
        {
        	if(control != null)
        	{
        		closeToolTip(null);
        		if(closeServerEvent != null){
        		   control.removeEventListener(closeServerEvent,closeToolTip);
        		}
        		if(displayServerEvent != null){
        		   control.removeEventListener(displayServerEvent,openTooltip);
        		}
        	}
        }
        
        public function initTimer():void
        {
        	if(timerKeyHide != null && timerKeyHide.running)
			{
				timerKeyHide.stop();
				timerKeyHide.removeEventListener(TimerEvent.TIMER,hideTooltip);				
			}
			timerKeyHide = new Timer(delay,1);
			timerKeyHide.addEventListener(TimerEvent.TIMER_COMPLETE,hideTooltip);
			timerKeyHide.start();
        }
        
        public function activateTimer():void
        {
        	timerIsActivated = true;       	
        }
        
        public function hideTooltip(event:Event):void
        {
        		timerKeyHide.stop();
				timerKeyHide.removeEventListener(TimerEvent.TIMER,hideTooltip);		
				closeToolTip(event);
        }
        
        public function get text():String {     
            return null;
        }
        
          public function set text(value:String):void
	    {
	         buildMessages();
	    }
        public function moveToolTip(event:Event):void
        {
        	buildMessages();
        	 var pt:Point = new Point(control.x,control.y);
             //pt = control.contentToGlobal(pt);
     	     this.x=0;//pt.x + control.width;//-this.width/2;
     	     

               this.y= 0;//pt.y-control.height;
               this.width= 300;
               this.height = 300;
              //this.percentHeight=100;
             //  this.percentWidth=100;
        }
        
        public function openTooltip(event:Event):void
        {
        	moveToolTip(event);
        	this.visible = true;
        	if(timerIsActivated)
            {
        		initTimer();
        	}
        }
        
        public function closeToolTip(event:Event):void
        {
        	this.visible = false;
        }
        
        public function  buildMessages():void
        {
        	     this.removeAllChildren();
 
        	var borderSet:Boolean = false;
        	if(StringUtil.trim(serverMessages).length>0)
        	{
        		var containerServerErr:HBox = new HBox();
		    	var textServerErr:Text = new Text();
		    	textServerErr.htmlText = serverMessages;
		    	var imgServerErr:Image = new Image();
		    	imgServerErr.source = errorClass;
		    	containerServerErr.addChild(imgServerErr);
		    	containerServerErr.addChild(textServerErr);
		    	containerServerErr.styleName="messageContainer";
		    	styleName = "errorMessage";
		    	borderSet = true;
		        
		    	addChild(containerServerErr);
        	}
        	 for each(var msgEr:String in messagesError)
		    {
		    	var containerErr:HBox = new HBox();
		    	var textErr:Text = new Text();
		    	textErr.htmlText =msgEr;
		    	var imgErr:Image = new Image();
		    	imgErr.source = errorClass;
		    	containerErr.addChild(imgErr);
		    	containerErr.addChild(textErr);
                containerErr.styleName="messageContainer";
		    	if(!borderSet)
		    	{
		    		styleName = "errorMessage";
		    		borderSet = true;
		    	}
		    	addChild(containerErr);
		    }
		     for each(var msgWarn:String in messagesWarn)
		    {
		    	var containerWarn:HBox = new HBox();
		    	var textWarn:Text = new Text();
		    	textWarn.htmlText = msgWarn;
		    	 var imgWarn:Image = new Image();
		    	imgWarn.source = warnClass;
		    	containerWarn.addChild(imgWarn); 
		    	containerWarn.addChild(textWarn);
		    	containerWarn.styleName="messageContainer";
		    	if(!borderSet)
		    	{
		    		styleName = "warnMessage";
		    		borderSet = true;
		    	}
		    	addChild(containerWarn);
		    }
		     for each(var msgInfo:String in messagesInfo)
		    {
		    	var containerInfo:HBox = new HBox();
		    	var textInfo:Text = new Text();
		    	textInfo.htmlText =msgInfo;
		    	var imgInfo:Image = new Image();
		    	imgInfo.source = infoClass;
		    	containerInfo.addChild(imgInfo);
		    	containerInfo.addChild(textInfo);
		    	containerInfo.styleName="messageContainer";
		    	if(!borderSet)
		    	{

		    		styleName = "infoMessage";
		    		borderSet = true;
		    	}
		    	addChild(containerInfo);
		    }		        
        }
        override protected function createChildren():void {
            super.createChildren();
            
              buildMessages();
        }
    }
}